﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Wp.DAL;

namespace Wp.BLL
{
    /// <summary>
    /// Summary description for Per_ActionsRoles
    /// </summary>
    public class TblPostsManager : TblPostsManagerBase
    {
        public TblPostsManager()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static TblPostsManager CreateInstant()
        {
            return new TblPostsManager();
        }
        public void UpdateParentOfChild(int Id, int ParentId)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@ParentId", ParentId));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_UpdateParentOfChildTblPosts", sqlparam);
        }
        public TblPostsEntity GetTblPostsEntity(DataRow dr, bool getChild)
        {
            TblPostsEntity item = new TblPostsEntity();
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();
            if (dr.Table.Columns.Contains("TextId"))
                item.TextId = dr["TextId"].ToString();
            if (dr.Table.Columns.Contains("Title"))
                item.Title = dr["Title"].ToString();
            if (dr.Table.Columns.Contains("PostType"))
                item.PostType = dr["PostType"].ToString();
            if (dr.Table.Columns.Contains("Summary"))
                item.Summary = dr["Summary"].ToString();
            if (dr.Table.Columns.Contains("Description"))
                item.Description = dr["Description"].ToString();
            if (dr.Table.Columns.Contains("ImageUrl"))
                item.ImageUrl = dr["ImageUrl"].ToString();
            if (dr.Table.Columns.Contains("Author"))
                item.Author = dr["Author"].ToString();
            if (dr.Table.Columns.Contains("Url"))
                item.Author = dr["Url"].ToString();
            if (dr.Table.Columns.Contains("CreatedBy"))
                item.CreatedBy = dr["CreatedBy"].ToString();
            if (dr.Table.Columns.Contains("CreatedDate"))
                item.CreatedDate = dr["CreatedDate"].ToDateTime();
            if (dr.Table.Columns.Contains("ModifiedBy"))
                item.ModifiedBy = dr["ModifiedBy"].ToString();
            if (dr.Table.Columns.Contains("ModifiedDate"))
                item.ModifiedDate = dr["ModifiedDate"].ToDateTime();
            if (dr.Table.Columns.Contains("DisplayOrder"))
                item.DisplayOrder = dr["DisplayOrder"].ToInt();
            if (dr.Table.Columns.Contains("ParentId"))
                item.ParentId = dr["ParentId"].ToInt();
            if (dr.Table.Columns.Contains("PostStatus"))
                item.PostStatus = dr["PostStatus"].ToString();
            if (dr.Table.Columns.Contains("CommentStatus"))
                item.CommentStatus = dr["CommentStatus"].ToString();
            if (dr.Table.Columns.Contains("PublishDate"))
                item.PublishDate = dr["PublishDate"].ToDateTime();
            if (getChild)
            {
                List<TblPostMetaEntity> ls = TblPostMetaManager.CreateInstant().SelectByPostId(item.Id);
                if (ls != null)
                {
                    item.PostMetas = new Hashtable();
                    foreach (var k in ls)
                    {
                        item.PostMetas.Add(k.MetaKey, k.MetaValue);
                    }
                }
            }
            return item;
        }
        public int InsertPost(string name, string url, int parentid, string des, string posttype)
        {
            try
            {
                TblPostsEntity item = new TblPostsEntity();
                item.Id = 0;
                item.TextId = UnicodeConversion.CreateInstant().GetStringId(name);
                item.Title = name;
                item.PostType = posttype;
                item.Summary = name;
                item.Description = des;
                item.ImageUrl = "";
                item.Url = url;
                item.CreatedBy = HttpContext.Current.User.Identity.Name;
                item.Author = HttpContext.Current.User.Identity.Name;
                item.CreatedDate = DateTime.Now;
                item.ModifiedBy = HttpContext.Current.User.Identity.Name;
                item.ModifiedDate = DateTime.Now;
                item.DisplayOrder = 1;
                item.ParentId = parentid;
                item.PostStatus = "publish";
                item.CommentStatus = "allow";//prohibit
                item.PublishDate = new DateTime(1900, 1, 1);
                return TblPostsManager.CreateInstant().InsertUpdate(item);
            }
            catch
            {
                return -1;
            }
        }
        public List<TblPostsEntity> SelectTblPostGetChildFormat(int? Id, string PostType)
        {
            List<TblPostsEntity> lsresult = new List<TblPostsEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            if (Id.HasValue)
                ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@PostType", PostType));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostGetChildFormat", sqlparam);

            if (dt != null && dt.Rows.Count > 0)
            {
                //TblPostsEntity item = new TblPostsEntity();
                TblPostsManager item = TblPostsManager.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTblPostsEntity(dt.Rows[i]));
            }
            return lsresult;
        }
        public List<TblPostsEntity> SelectTblPostByParent(int ParentId, string PostType)
        {
            List<TblPostsEntity> lsresult = new List<TblPostsEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            //if (Id.HasValue)
            ls.Add(new SqlParameter("@ParentId", ParentId));
            ls.Add(new SqlParameter("@PostType", PostType));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByParent", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
            {
                //TblPostsEntity item = new TblPostsEntity();
                TblPostsManager item = TblPostsManager.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTblPostsEntity(dt.Rows[i]));
            }
            return lsresult;
        }
        public string GetUniqueTextIdFromUnicodeText(string UnicodeText)
        {
            string strSql = @"  SELECT TextId FROM TblPosts tn
                                WHERE tn.TextId=@TextId And PostType='post'";
            string textId = WebUtility.GetUniqueTextIdFromUnicodeText(UnicodeText, "@TextId", strSql
                , SqlHelper.WpConnectString, 255);
            return textId;
        }
        public List<TblPostsEntity> SelectTblPostByPostType(string PostType, int PageSize, int PageIndex, out int TotalRow)
        {
            TotalRow = 0;
            List<TblPostsEntity> lsresult = new List<TblPostsEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            SqlParameter sp = new SqlParameter("@TotalRow", TotalRow);
            sp.Direction = System.Data.ParameterDirection.Output;
            ls.Add(sp);

            ls.Add(new SqlParameter("@PostType", PostType));
            ls.Add(new SqlParameter("@PageSize", PageSize));
            ls.Add(new SqlParameter("@PageIndex", PageIndex));

            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByPostType", sqlparam);
            TotalRow = int.Parse(sqlparam[0].Value.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                //TblPostsEntity item = new TblPostsEntity();
                TblPostsManager item = TblPostsManager.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTblPostsEntity(dt.Rows[i]));
            }
            return lsresult;
        }
        public List<TblPostsEntity> SelectTblPostByPostType(string PostType, string TermTaxonomyTextId, string Taxonomy, int PageSize, int PageIndex, out int TotalRow)
        {
            TotalRow = 0;
            List<TblPostsEntity> lsresult = new List<TblPostsEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            SqlParameter sp = new SqlParameter("@TotalRow", TotalRow);
            sp.Direction = System.Data.ParameterDirection.Output;
            ls.Add(sp);
            ls.Add(new SqlParameter("@PostType", PostType));
            ls.Add(new SqlParameter("@TermTaxonomyTextId", TermTaxonomyTextId));
            ls.Add(new SqlParameter("@Taxonomy ", Taxonomy));
            ls.Add(new SqlParameter("@PageSize", PageSize));
            ls.Add(new SqlParameter("@PageIndex", PageIndex));

            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByTermTaxonomyTextId", sqlparam);
            TotalRow = int.Parse(sqlparam[0].Value.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                //TblPostsEntity item = new TblPostsEntity();
                TblPostsManager item = TblPostsManager.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTblPostsEntity(dt.Rows[i]));
            }
            return lsresult;
        }

        public List<TblPostsEntity> SelectTblPostByTags(string Tags, int Top)
        {
            List<TblPostsEntity> lsresult = new List<TblPostsEntity>();
            List<SqlParameter> ls = new List<SqlParameter>();
            if (!Tags.Equals(""))
                ls.Add(new SqlParameter("@Tags", Tags));
            ls.Add(new SqlParameter("@Top", Top));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPostByTags", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
            {
                //TblPostsEntity item = new TblPostsEntity();
                TblPostsManager item = TblPostsManager.CreateInstant();
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(item.GetTblPostsEntity(dt.Rows[i]));
            }
            return lsresult;
        }
        public TblPostsEntity SelectOneByTextId(string TextId)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@TextId", TextId));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.WpConnectString, CommandType.StoredProcedure, "wp_SelectTblPost", sqlparam);
            if (dt != null && dt.Rows.Count > 0)
                return GetTblPostsEntity(dt.Rows[0]);
            return null;
        }
    }
}
